package com.eightgroups.tces.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.controller.BaseController;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.sys.entity.User;
import com.eightgroups.tces.entity.*;
import com.eightgroups.tces.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @User Administrator
 * @Date Create in 10:02/2017/11/29
 * @Descriptton { }
 **/
@Controller
@RequestMapping("/tces")
public class TcesIndexController extends BaseController {
    @Autowired
    private ICoursePlanService iCoursePlanService;
    @Autowired
    private IQtypeService iQtypeService;
    @Autowired
    private IQtypeIndicatorService iQtypeIndicatorService;
    @Autowired
    private IIndicatorService iIndicatorService;
    @Autowired
    private ITcesResultService iTcesResultService;
    @RequestMapping("/index.html")
    public String index(Model model){
        Page<CoursePlanVo> coursePlanVoPage = new Page<>(1,20);
        EntityWrapper<CoursePlanVo> entityWrapper = new EntityWrapper<>();
        Page<CoursePlanVo> page = iCoursePlanService.selectPageVo(coursePlanVoPage,entityWrapper);

        User user =  getCurrentUser();
        EntityWrapper<Qtype> qtypeEntityWrapper = new EntityWrapper<>();
        qtypeEntityWrapper.eq("type",user.getStatus());



        model.addAttribute("coursePlanVos", page.getRecords());
        model.addAttribute("qtype", iQtypeService.selectOne(qtypeEntityWrapper));
        return "tces/index";
    }
    @RequestMapping("/teacher")
    public String teacher(String id, Model model){
        Page<CoursePlanVo> coursePlanVoPage = new Page<>(1,1);
        EntityWrapper<CoursePlanVo> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("cp.id",id);
        Page<CoursePlanVo> page = iCoursePlanService.selectPageVo(coursePlanVoPage,entityWrapper);
        //查询相应的问卷
        User user =  getCurrentUser();
        EntityWrapper<Qtype> qtypeEntityWrapper = new EntityWrapper<>();
        qtypeEntityWrapper.eq("type",user.getStatus());

        EntityWrapper<TcesResult> tcesResultEntityWrapper = new EntityWrapper<>();
        tcesResultEntityWrapper.eq("uid",user.getId());
        TcesResult tcesResult = iTcesResultService.selectOne(tcesResultEntityWrapper);
        if(tcesResult==null){
            model.addAttribute("Result", 0);
        }else {
            model.addAttribute("Result", 1);
        }
        model.addAttribute("coursePlanVo", page.getRecords());
        model.addAttribute("qtype", iQtypeService.selectOne(qtypeEntityWrapper));
        return "tces/teacher";
    }
    @RequestMapping("/teacherPage")
    public String teacherPage(String qid,String cpId, Model model){
        EntityWrapper<QtypeIndicator> qtypeIndicatorEntityWrapper = new EntityWrapper<>();
        qtypeIndicatorEntityWrapper.eq("qid",qid);
        List<QtypeIndicator> qtypeIndicators = iQtypeIndicatorService.selectList(qtypeIndicatorEntityWrapper);
        StringBuffer ids = new StringBuffer();
        for(QtypeIndicator qtypeIndicator :qtypeIndicators){
                ids.append(qtypeIndicator.getIid()+",");
        }
        EntityWrapper<Indicator> indicatorEntityWrapper = new EntityWrapper<>();
        indicatorEntityWrapper.in("id",ids.toString());
        List<Indicator> indicators = iIndicatorService.selectList(indicatorEntityWrapper);
        model.addAttribute("indicators",indicators);
        model.addAttribute("qId",qid);
        model.addAttribute("cpId",cpId);
        return "tces/teacherpage";
    }
    @RequestMapping("/comment")
    @ResponseBody
    public JsonResult comment(TcesResult tcesResult, Model model){
        User user =  getCurrentUser();
        tcesResult.setUid(user.getId());
        return iTcesResultService.insert(tcesResult)?renderSuccess("添加成功"):renderError("添加失败");
    }
    @RequestMapping("/mypj")
    public String mypj(Model model){
        User user =  getCurrentUser();
        TcesResult tcesResult = new TcesResult();
        tcesResult.setUid(user.getId());
        List<TcesResultVo> tcesResults = iTcesResultService.select(tcesResult);

        TcesResultVo tcesResultVo = new TcesResultVo();
        tcesResultVo.setTname(user.getUsername());
        List<TcesResultVo> tcesResultVos = iTcesResultService.selectByTname(tcesResultVo);
        double total = 0;
        for (TcesResultVo t : tcesResultVos){
            total+=t.getSum();
        }
        model.addAttribute("user",user);
        model.addAttribute("tcesResults",tcesResults);
        model.addAttribute("tcesResultVos",tcesResultVos);
        model.addAttribute("total",total);
        return "tces/mypj";
    }
}
